const mongoConnected = require("../lib/mongoose_connect");

const mongoModels = require("../lib/mongoose_schemas");
const lodash = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const DATA1 = require("../test/210220_safetyzonevalue-gaoshou.json");
const DATA2 = require("../test/210221_yulinplayers.json")


mongoConnected.then(async $ => {
  // let q1 = (await mongoModels.TB21_luodian.find({
  //   "map_name": "Savage_Main"
  // }, {
  //   "luodian": "$luodian",
  //   "safetyzone_value": "$safetyzone_value",
  //   "ranking": "$ranking",
  //   player_name:"$player_name",
  //   "_id": 0
  // })).map(e => e._doc);
  // await fs.writeFile(path.join(__dirname, "../test/210220_验证我的safetyzonevalue.json"), JSON.stringify(q1));
  // debugger
  let gaoshous = DATA2.filter(e=>{
    return e.matches_count>=10 && e.qianwulv>=0.45
  });
  console.log(gaoshous.length,"个高手")
  debugger
  let gaoshoudian  = DATA1.filter(e=>!e.is_gaoshou);
  let pianlanse = gaoshoudian.filter(e=>e.safetyzone_value>=20)
  // for(let d of DATA1){
  //   let name = d.player_name;
  //   if(gaoshous.find(e=>e.player_name===name)){
  //     d['is_gaoshou'] = 1
  //   }else{
  //     d['is_gaoshou'] = 0;
  //   }
  //   // debugger
  // }
  debugger
  // await fs.writeFile(path.join(__dirname, "../test/210220_safetyzonevalue-gaoshou.json"), JSON.stringify(DATA1));

  return
  let players = (await mongoModels.TB21_luodian.aggregate(
    [
      {
        "$match": {
          "map_name": "Savage_Main"
        }
      },
      {
        "$project": {
          "player_name": "$player_name",
          "_id": 0
        }
      },
      {
        "$group": {
          "_id": null,
          "distinct": {
            "$addToSet": "$$ROOT"
          }
        }
      },
      {
        "$unwind": {
          "path": "$distinct",
          "preserveNullAndEmptyArrays": false
        }
      },
      {
        "$replaceRoot": {
          "newRoot": "$distinct"
        }
      }
    ]
  ));
  debugger
  // .map(e => e.player_name);
  // let gaoshous = [];
  for (let player of players) {
    let dians = await mongoModels.TB21_luodian.find({
      map_name: "Savage_Main",
      player_name: player.player_name
    });
    let matches_count = dians.length;
    let qianwu = dians.filter(e => e.ranking <= 5).length;
    let qianwulv = qianwu / matches_count;
    player['matches_count'] = matches_count
    player['qianwu'] = qianwu;
    player['qianwulv'] = qianwulv
    console.log(player.player_name,"done")
    // debugger

    // if (dians.length > 10) {
    //   debugger
    // }

  }
  // .map(e => e._doc);
  debugger
  await fs.writeFile(path.join(__dirname, "../test/210221_yulinplayers.json"), JSON.stringify(players));
  // await fs.writeFile(path.join(__dirname, "../test/210221_yulinplayers.json"), JSON.stringify(q));
  debugger
  let gaoshou = DATA.filter(e => {
    return e.ranking <= 5
  });
  debugger
  return
  let q = (await mongoModels.TB21_luodian.find({
    "map_name": "Savage_Main"
  }, {
    "luodian": "$luodian",
    "safetyzone_value": "$safetyzone_value",
    "ranking": "$ranking",
    player_name:"$player_name",
    "_id": 0
  })).map(e => e._doc);
  await fs.writeFile(path.join(__dirname, "../test/210220_验证我的safetyzonevalue.json"), JSON.stringify(q));
  debugger
})